<template>
  <div>
    <div style="display:flex">
      <div class="search-Box">
        <div class="left-box">
          <div class="item">
            <div class="in-item">
              <span class="label" style="width:50px">学年:</span>
              <el-input size="mini" placeholder="输入搜索学年" v-model="tempSearchParam.xn"></el-input>
            </div>
            <div class="in-item">
              <span class="label" style>班级名称:</span>
              <el-input size="mini" placeholder="输入搜索班级名称" v-model="tempSearchParam.class"></el-input>
            </div>
            <div class="in-item">
              <span class="label" style>所属专业:</span>
              <el-select
                size="mini"
                v-model="searchSV"
                clearable
                placeholder="请选择"
                @change="cOptionChange"
                @clear="cClear"
              >
                <el-option
                  v-for="item in cOption.list"
                  :key="item._id"
                  :label="item.name"
                  :value="item._id"
                ></el-option>
              </el-select>
              <!-- <el-input size="mini" placeholder="输入搜索" v-model="tempSearchParam.specialty"></el-input> -->
            </div>
          </div>
        </div>
        <div class="right-box">
          <div class="item">
            <el-button
              style="margin-left:20px"
              size="mini"
              type="primary"
              icon="el-icon-search"
              @click="search"
            >搜索</el-button>
            <el-upload
              class="upload"
              ref="upload"
              action="notUse"
              :file-list="fileList"
              :auto-upload="false"
              :http-request="uploadFile"
              :before-upload="beforeUpload"
              accept=".xls, .xlsx"
              :limit="1"
            >
              <el-button slot="trigger" size="small" type="primary">导入班级列表</el-button>
              <el-button
                style="margin-left: 10px;"
                size="small"
                type="success"
                @click="submitUpload"
              >上传到服务器</el-button>
              <div slot="tip" class="el-upload__tip">
                <i style="font-style:normal;font-weight:bold">提示</i>:此为导入多条教师信息，只能上传xls/xlsx文件，且不超过2mb
              </div>
            </el-upload>
          </div>
        </div>
      </div>
      <!-- <div style="display:flex;align-items:center;">
      
        <div style="display:flex;align-items:center">
          <span style="width:50px;font-size:14px;">学年:</span>
          <el-input size="mini" placeholder="输入搜索学年，例如：2019" v-model="tempSearchParam.xn"></el-input>
        </div>
        <div style="display:flex;align-items:center">
          <span style="font-size:14px;width:70px;text-align:right;">班级名字:</span>
          <el-input
            style="width:300px;margin-left:10px"
            size="mini"
            placeholder="输入班级名称，例如19计算机科学与技术四班"
            v-model="tempSearchParam.class"
          ></el-input>
        </div>
        <div style="display:flex;align-items:center;margin-left:10px">
          <span style="font-size:14px;width:100px">所属专业:</span>
          <el-input size="mini" placeholder="输入搜索学年" v-model="tempSearchParam.Specialty"></el-input>
        </div>
        <div>
          <el-button style="margin-left:20px" size="mini" type="primary" icon="el-icon-search">搜索</el-button>
        </div>
      </div>-->
    </div>
    <el-table :data="classList" stripe style="width: 100%;height:600px">
      <el-table-column prop="nid" label="班级编号"></el-table-column>
      <el-table-column prop="name" label="班级名字"></el-table-column>
      <el-table-column prop="year" label="归属学年"></el-table-column>
      <el-table-column prop="superior.name" label="所属专业"></el-table-column>
      <el-table-column prop="dean" label="班主任"></el-table-column>
      <el-table-column fixed="right" label="操作" width="200">
        <template slot-scope="scope">
          <el-button size="mini" @click="checkInfo(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="foot-box">
      <div style="width:50%">
        <el-button type="success" @click="rufush">刷新</el-button>
      </div>
      <div style="width:50%">
        <el-pagination
          background
          :current-page="option.nowPage"
          layout="prev, pager, next"
          @current-change="handleCurrentChange"
          :page-size="option.size"
          :total="option.counts"
        ></el-pagination>
      </div>
    </div>

    <div :class="['alertBg',isSubShow?'show':'']">
      <el-form class="alertForm" label-width="80px">
        <el-form-item label="班级名称">
          <el-input v-model="submitForm.name"></el-input>
        </el-form-item>
        <el-form-item label="归属学年">
          <el-input v-model="submitForm.year"></el-input>
        </el-form-item>
        <el-form-item label="所属专业">
          <el-select v-model="cOption.value" placeholder="请选择" @change="cOptionChange">
            <el-option
              v-for="item in cOption.list"
              :key="item._id"
              :label="item.name"
              :value="item._id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="班主任">
          <el-input v-model="submitForm.dean"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="formSubmit">确定</el-button>
          <el-button @click="close">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div :class="['alertBg',isEdit?'show':'']">
      <el-form class="alertForm" label-width="80px">
        <el-form-item label="班级名称">
          <el-input v-model="editForm.name"></el-input>
        </el-form-item>
        <el-form-item label="归属学年">
          <el-input v-model="editForm.year"></el-input>
        </el-form-item>
        <el-form-item label="所属专业">
          <el-select v-model="cOption.value" placeholder="请选择" @change="cOptionChange">
            <el-option
              v-for="item in cOption.list"
              :key="item._id"
              :label="item.name"
              :value="item._id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="班主任">
          <el-input v-model="editForm.dean"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="editSubmit">确定</el-button>
          <el-button @click="close">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import XLSX from "xlsx";
import { handleMessage, handlePageList, readExcel } from "../../plugins";
export default {
  data() {
    return {
      classList: [],
      searchParam: {},
      tempSearchParam: {},
      option: { size: 10, counts: 0, nowPage: 1 },
      cOption: { value: "", list: [] },
      submitForm: {},
      editForm: {},
      isSubShow: false,
      // teacherNumList: { value: "", list: [] },
      isEdit: false,
      editId: "",
      fileList: [],
      batchPushList: [],
      specialyNumList: [],
      searchSV: "",
      isSearch: false
    };
  },
  methods: {
    rufush() {
      this.option.nowPage = 1;
      this.searchParam.page = 1;
      this.isSearch = false;
      this.getClassList();
      this.searchParam.class = "";
      this.searchParam.superior = "";
      this.searchParam.year = "";
      this.tempSearchParam = {};
      this.searchSV = "";
    },
    async search() {
      this.option.nowPage = 1;
      this.searchParam.page = 1;
      this.searchParam.year = this.tempSearchParam.xn;

      // let classres = await this.$http.get(
      //   "rest/Class?name=" + this.tempSearchParam.class
      // );
      this.searchParam.name=this.tempSearchParam.class;
      // if (classres.data.length == 1) {
      //   this.searchParam._id = classres.data[0]._id;
      //   // ? classres.data[0]._id
      //   // : "";
      // } else {
      //   // if (this.tempSearchParam.xn || this.tempSearchParam.superior) {
      //   //   this.searchParam._id = "";
      //   // } else {
      //   //   this.searchParam._id = "error";
      //   // }
      //   this.searchParam._id = "";
      // }
      this.searchParam.superior = this.tempSearchParam.superior;
      console.log(this.searchParam.superior);
      handlePageList(this, "searchList/Class", "classList");
    },
    submitUpload() {
      this.$refs.upload.submit();
    },
    async uploadFile() {
      const loading = this.$loading({
        lock: true,
        text: "正在上传数据中，请稍等~",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      });
      let res = await this.$http.post(
        "insertRest/insertMore/Class",
        this.batchPushList
      );
      loading.close();
      if (res.data.success) {
        this.$message.success(res.data.message);
      } else {
        this.$message.error(res.data.message);
      }
      this.getClassList();
      this.fileList = [];
    },
    beforeUpload(file) {
      let _this = this;
      //使返回的值变成Promise对象，如果校验不通过，则reject，校验通过，则resolve
      return new Promise(function(resolve, reject) {
        readExcel(_this, file, XLSX, "class").then(
          res => {
            const isLt2M = file.size / 1024 / 1204 < 2;
            if (!isLt2M) {
              _this.$message.error("文件大小不能超过2M！");
            }
            if (res && isLt2M) resolve(true);
            reject(false);
          },
          error => {
            //此处为reject返回时候
            _this.$message.error(error);
            reject(false);
          }
        );
      });
    },
    async formSubmit() {
      if (!this.submitForm.name) {
        this.$message({
          type: "warning",
          message: "请输入班级名！"
        });
        return;
      }
      if (!this.cOption.value) {
        this.$message({
          type: "warning",
          message: "请选择所属专业！"
        });
        return;
      }
      if (!this.submitForm.year) {
        this.$message({
          type: "warning",
          message: "请输入归属学年"
        });
        return;
      }
      if (!this.submitForm.dean) {
        this.$message({
          type: "warning",
          message: "请输入班主任姓名"
        });
        return;
      }
      let res = await this.$http.post("rest/Class", this.submitForm);
      handleMessage(this, res.data);
      this.getClassList();
      this.close();
    },
    async editSubmit() {
      console.log(this.editForm);

      if (!this.editForm.name) {
        this.$message({
          type: "warning",
          message: "请输入班级名！"
        });
        return;
      }
      if (!this.cOption.value) {
        this.$message({
          type: "warning",
          message: "请选择所属专业！"
        });
        return;
      }
      if (!this.editForm.year) {
        this.$message({
          type: "warning",
          message: "请输入归属学年"
        });
        return;
      }
      if (!this.editForm.dean) {
        this.$message({
          type: "warning",
          message: "请输入班主任姓名"
        });
        return;
      }
      let res = await this.$http.put(
        `rest/Class/${this.editId}`,
        this.editForm
      );
      handleMessage(this, res.data);
      this.getClassList();
      this.close();
    },
    cOptionChange(e) {
      this.submitForm.superior = e;
      this.editForm.superior = e;
      this.tempSearchParam.superior = e;
    },
    cClear() {
      this.tempSearchParam.superior = "";
    },
    close() {
      this.isSubShow = false;
      this.submitForm = {};
      this.cOption.value = "";
      this.editForm = {};
      this.isEdit = false;
    },
    addAlert() {
      this.isSubShow = true;
    },
    checkInfo(index, row) {
      this.isEdit = true;
      this.editId = row._id;
      this.cOption.value = row.superior.name;
      this.editForm = {
        name: row.name,
        dean: row.dean,
        year: row.year,
        superior: row.superior._id
      };
    },
    handleDelete(index, row) {
      this.$confirm(`是否确定要删除 "${row.name}"`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(async () => {
        const res = await this.$http.delete(`rest/Class/${row._id}`);
        handleMessage(this, res.data);
        this.getClassList();
      });
    },
    handleCurrentChange(e) {
      this.searchParam.page = e;
      if (this.isSearch) {
        handlePageList(this, "searchList/Class", "classList");
      } else {
        this.getClassList();
      }
    },
    async getSpeciallyList() {
      let res = await this.$http.get("rest/Specialty");
      this.cOption.list = res.data;
      this.specialyNumList = res.data;
    },
    getClassList() {
      handlePageList(this, "list/Class", "classList");
    }
    // async getTeacherNumList() {
    //   let res = await this.$http.get("TeacherNumList");
    //   if (!res.data.success) {
    //     this.$message.error(res.data.message);
    //     return;
    //   }
    //   this.teacherNumList.list = res.data.list;
    // }
  },
  created() {
    this.getClassList();
    this.getSpeciallyList();
  }
};
</script>

<style scoped>
.alertForm {
  padding-left: 18px;
  padding-bottom: 5px;
}
.alertBg {
  display: none;
}
.show {
  display: flex;
}
.upload {
  margin-left: 50px;
}
.search-Box {
  box-sizing: border-box;
  display: flex;
  align-items: center;
  width: 100%;
  /* margin-left: 100px; */
  justify-content: space-between;
}
.left-box {
  width: 60%;
}
.right-box {
  width: 40%;
}
.search-Box .item {
  margin-top: 20px;
  display: flex;
  align-items: center;
}
.search-Box .item:nth-child(1) {
  margin-top: 0;
}
.search-Box .in-item {
  display: flex;
  align-items: center;
}

.search-Box .in-item {
  margin-left: 50px;
}
.search-Box .in-item:nth-child(1) {
  margin: 0;
}
.in-item .label {
  width: 100px;
  font-size: 14px;
  text-align: right;
  margin-right: 10px;
}
.foot-box {
  display: flex;
  margin-top: 20px;
  align-items: flex-end;
  padding: 0 50px;
  padding-right: 80px;
  justify-content: space-between;
}
</style>